package com.zueb.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zueb.pojo.Classes;
import com.zueb.pojo.Faculty;
import com.zueb.service.FacultyService;
import com.zueb.service.impl.FacultyServiceImpl;
import com.zueb.util.ResponseWriterUtils;
import com.zueb.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "FacultyServlet", value = "/FacultyServlet")
public class FacultyServlet extends HttpServlet {

    private FacultyService facultyService = new FacultyServiceImpl();
    static String message = "";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("showFaculty")) {
            showFaculty(request, response);  //查询所有学院信息
        } else if (action.equals("addFaculty")) {
            addFaculty(request, response);  //添加学院
        } else if (action.equals("deleteFaculty")) {
            deleteFaculty(request, response);  //删除学院
        } else if (action.equals("addFacultyUi")) {
            addFacultyUi(request, response);  //添加学院页面
        } else if (action.equals("showClasses")) {
            showClasses(request, response);   //展示班级
        } else if (action.equals("showFacultyByPage")) {
            showFacultyByPage(request, response);   //展示班级
        } else if (action.equals("editFaculty")) {
            editFaculty(request, response);   //展示班级
        } else if (action.equals("showClassesByPage")) {
            showClassesByPage(request, response);   //展示班级
        }else if (action.equals("addClasses")) {
            addClasses(request, response);   //展示班级
        } else if (action.equals("editClasses")) {
            editClasses(request, response);   //展示班级
        }

    }

    /**
     * 修改班级
     * @param request
     * @param response
     */
    private void editClasses(HttpServletRequest request, HttpServletResponse response) {
        String className = request.getParameter("className");
        int classId = Integer.parseInt(request.getParameter("classId"));
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));

        int flag = facultyService.editClasses(classId, className, facultyId);

        ResponseWriterUtils.writerFlag(flag, response);
    }

    /**
     * 添加班级
     * @param request
     * @param response
     * @throws IOException
     */
    private void addClasses(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String className = request.getParameter("className");
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));

        int flag = facultyService.addClasses(className, facultyId);

        ResponseWriterUtils.writerFlag(flag, response);
    }

    /**
     * 带页码查询班级
     * @param request
     * @param response
     */
    private void showClassesByPage(HttpServletRequest request, HttpServletResponse response) {
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));


        List<Classes> classesByPage = facultyService.getClassesByPage(curPage, pageSize, facultyId);
        int count = facultyService.getClassesCount(facultyId);
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("classes", classesByPage);

        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 编辑学院
     * @param request
     * @param response
     * @throws IOException
     */
    private void editFaculty(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String facultyName = request.getParameter("facultyName");
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        //int curPage = Integer.parseInt(request.getParameter("curPage"));

        int flag = facultyService.editFaculty(facultyId, facultyName);

        if (flag == 1) {
            response.getWriter().write("true");
        } else {
            response.getWriter().write("false");
        }
    }

    /**
     * 带页码查询学院
     *
     * @param request
     * @param response
     */
    private void showFacultyByPage(HttpServletRequest request, HttpServletResponse response) {
        Integer curPage = Integer.parseInt(request.getParameter("curPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        //调用方法查询数据库
        List<Faculty> facultyByPage = facultyService.getFacultyByPage(curPage, pageSize);
        int count = facultyService.getFacultyCount();
        Map<String, Object> map = new HashMap<>();
        map.put("faculty", facultyByPage);
        map.put("count", count);

        //传回页面参数
        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 查询所有班级
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void showClasses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //需要查找的学院id
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        //从数据库中得到班级列表
        List<Classes> classes = facultyService.getClassesByFaculty(facultyId);
        String jsonString = JSON.toJSONString(classes);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(jsonString);
//        //将数据传入页面
//        request.setAttribute("classes", classes);
//        //跳转页面
//        request.getRequestDispatcher("showClasses.jsp").forward(request, response);
    }


    /**
     * 添加学院页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addFacultyUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        SessionUtils.isSession(request, response);
        //跳转页面
        request.getRequestDispatcher("addFaculty.jsp").forward(request, response);
    }

    /**
     * 删除学院
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void deleteFaculty(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        SessionUtils.isSession(request, response);
        //需要删除的学院ID
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        //调用Service
        Integer flag = facultyService.deleteFacultyById(facultyId);
        Map<String, Object> map = new HashMap<>();
        map.put("curPage", curPage);

        if (flag == 1) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        ResponseWriterUtils.writer(map, response);
//        //把信息放入页面
//        request.setAttribute("message", message);
//        //跳转页面
//        request.getRequestDispatcher("message.jsp").forward(request, response);
    }

    /**
     * 添加学院
     *
     * @param request
     * @param response
     */
    private void addFaculty(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        SessionUtils.isSession(request, response);
        //接受页面传来的信息
        String facultyName = request.getParameter("facultyName");

        String curPage = request.getParameter("curPage");
        String pageSize = request.getParameter("pageSize");


        //封装数据
        Faculty faculty = new Faculty();
        faculty.setFacultyName(facultyName);
        //查询学院是否存在
        Faculty getFaculty = facultyService.getFacultyByName(facultyName);
        if (getFaculty == null) {
            //调用Service
            facultyService.addFaculty(faculty);
            response.getWriter().write("true");

//            message = "<script>self.opener.location.reload();window.close();window.opener.alert('添加成功！');</script>";
        } else {
            response.getWriter().write("false");
//            message = "<script>alert('添加失败！已存在该学院，无法添加！');window.open('FacultyServlet?action=addFacultyUi','_top');</script>";
        }
//        //把信息放入页面
//        request.setAttribute("message", message);
//        //跳转页面
//        request.getRequestDispatcher("message.jsp").forward(request, response);
    }

    /**
     * 查询所有学院信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void showFaculty(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        //SessionUtils.isSession(request, response);
        //获取全部学院的信息
        List<Faculty> faculties = facultyService.getAllFaculty();

        String jsonString = JSON.toJSONString(faculties);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(jsonString);
//        //将信息放入页面
//        request.setAttribute("faculties", faculties);
//        //跳转页面
//        request.getRequestDispatcher("showFaculty.jsp").forward(request, response);
    }
}
